package com.kstech.mp3604.ui.startBoot

import android.app.Service
import android.content.Intent
import android.os.IBinder
import com.kstech.mp3604.base.MainActivity
import java.util.Timer
import java.util.TimerTask


class BootService : Service(){
    override fun onBind(p0: Intent?): IBinder? {
        return null
    }

    private var timer: Timer? = null

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        // 开启一个定时器，每隔一段时间检查设备状态
        timer = Timer()
        timer!!.schedule(object : TimerTask() {
            override fun run() {
                // 检查设备状态
                val isBootCompleted: Boolean = isBootCompleted()
                if (isBootCompleted) {
                    // 启动我们的应用
                    val launchIntent = Intent(getApplicationContext(), MainActivity::class.java)
                    launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    startActivity(launchIntent)
                }
            }
        }, 0, 1000) // 每隔1秒检查一次设备状态
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        // 取消定时器
        if (timer != null) {
            timer!!.cancel()
            timer = null
        }
        super.onDestroy()
    }

    private fun isBootCompleted(): Boolean {
        // 检查设备状态，例如通过读取系统属性或查询系统服务
        return true // 假设设备状态正常
    }
}